<?php
class Router_Router{

	private $sUrl;
	private $aParams = array();
	private $pParams = array();
	private $sActionDefault ;
	private $sModuleDefault = DEFAULT_MODULE;
	private $sLangDefault = DEFAULT_LANG;
       
	public $language;
	public $module;
	public $action;
	public $params;
	static $registry;
	public $widok;

	function __construct(){
		//$this->registry = Zend_Registry::getInstance();
		//echo $this->registry->tree;
		
		
		
	}
	
	private function getAlias(){
		if(!isset($_SERVER['PATH_INFO'])){
			$_SERVER['PATH_INFO'] = '';
		}
		$a = substr($_SERVER['PATH_INFO'], 1 );
		if(URL_SUFFIX){
			$alias = substr($a, 0, -strlen(URL_SUFFIX)-1);  
		}else{
			$alias = $a;
		}
		//if(!isset($_SERVER['PATH_INFO'])){$_SERVER['PATH_INFO'] = '';}
		$node = Node::getNode(null,$alias,null);
		if($node){
			return $node->url;
		}else{
			return false;
		}
	}
	
	function routerr(){
		if(!isset($_SERVER['PATH_INFO'])){$_SERVER['PATH_INFO'] = '';}
		/*$urlAlias = $this->getAlias();
		if($urlAlias){
			$this->sUrl = ltrim($urlAlias,'/');
		}else{
		*/
			echo $this->sUrl = substr($_SERVER['PATH_INFO'],1);
		/*
		}
		*/
		/*
		global $langSetup;
		global $modules;
		$this->convertUrl();
		$this->pParams = $this->aParams;
		if(!empty($this->pParams[0])){
			// czy jezyk jest na liscie
    		if(array_key_exists($this->pParams[0],$langSetup)){
    			//jest na liscie
    			$this->language = $this->pParams[0];
    			//sprawdzamy modul
    			if(!empty( $this->pParams[1]) && array_key_exists($this->pParams[1],$modules)){
    				//jest taki modul
    				$this->module = $this->pParams[1];
    				$this->params = array_splice($this->pParams,2);
				}else{
					$this->module = $this->sModuleDefault;
					$this->params = array_splice($this->pParams,1);
    			}
    		}else{
    			//nie ma jezyka na liscie
				$this->language = $this->sLangDefault;
				// czy to modul
				if(array_key_exists($this->pParams[0],$modules)){
    				//jest taki modul
    				echo $this->module = $this->pParams[0];
    				$this->params = array_splice($this->pParams,1);
    			}else{
    				//nie ma takiego modulu
    				$this->module = $this->sModuleDefault;
    				$this->params = $this->pParams;
    			}
    		}
    	}else{
			//wszystko domyslne
    		$this->language = $this->sLangDefault;
    		$this->module = $this->sModuleDefault;
    	}
		$this->sActionDefault = $langSetup[$this->language]['artHomepage'];
		if(!empty( $this->params[0])){
			echo $this->action = $this->params[0];
			echo $this->params[0];
    	}else{
			echo $this->action = $this->sActionDefault;
    	}
    	if(sizeof($this->params) > 0){
			array_shift($this->params);
		}
		*/
		
			echo "<br>";
			echo $this->sUrl = substr($_SERVER['PATH_INFO'],1);
			echo "<br>";
			$this->convertUrl();
			dump($this->aParams);
			
		Registry_Registry::storeObject('module', ucfirst($this->aParams[1]));
			
		$this->module = ucfirst($this->aParams[1]).'_'.ucfirst($this->aParams[1]);
		
		if($this->aParams[2]){
		$this->action = $this->aParams[2];
		}else{
			$this->action = 'index';
		}
		
		echo $sModuleName = $this->module;
		echo $this->action;
		
		
		
		
		Registry_Registry::storeObject('action', $this->action);
		
		
		//Zend_Registry::set('module', $this->module.'_'.$this->module);
		//Zend_Registry::set('action', $this->action);
		dump(__DIR__);
		
	//require_once(LIBRARY.'/'.ucfirst($sModuleName)."/".$sModuleName."_module.php");
		
	
		
		
	}
	
	public function getWidok(){
		return $this->widok;
	}
	
	public function route(){
		$this->loadModule($this->module);
		
	}
    
    function setUrl($sUrl){
		return $this->sUrl = $sUrl;
	}
    
    function setActionDefault($value){
        return $this->sActionDefault = $value;
    }
	
    function setModuleDefault($value){
        return $this->sModuleDefault = $value;
    }
	
	function setLangDefault($value){
        return $this->sLangDefault = $value;
    }
    
    function convertUrl(){
    	echo "<br>URL: ".urlencode($this->sUrl)."<br>";
    	$str = preg_replace('!/$!',null,$this->sUrl);
    	echo "<br>URL: $str<br>";
		return ($this->aParams = preg_split('/[\/\,\:\^\*\"\']/',$this->sUrl,-1));
    }

    function getParam($sId){
        return (isset($this->aParams[ $sId ] ) ? $this->aParams[ $sId ] : '');
    }
	
	function getModule($sId = 0){
        if($sId == 1){
			return (!empty($this->aParams[0]) ? end($this->aParams) : $this->sModuleDefault);
        }else{
			return (!empty($this->aParams[1]) ? $this->aParams[1] : $this->sModuleDefault);
		}
	}
   
	function getAction($sId = 0){
		if($sId == 1){
			return (!empty($this->aParams[0]) ? end($this->aParams) : $this->sActionDefault);
        }else{
            return (!empty($this->aParams[2]) ? $this->aParams[2] : $this->sActionDefault);
		}
	}
    
    function getLang($sId = 0){
		if(!empty($this->aParams[0])){
			return end($this->aParams);
        }else{
			return $this->sLangDefault;
        }
	}
    
	function getDomain(){
        return $_SERVER["HTTP_HOST"];
    }
       
    function getUrl(){
        return $this->sUrl;
    }
      
    function createUrl($sName = ''){
        $tDirName = explode( '/', $_SERVER['SCRIPT_NAME']);
        $tFileName = end($tDirName);
		return $tFileName .'/'. $sName;
    }
		
	function loadModule($sModuleName){
		//$registry  = $this->registry;
		
		
		global $path;
		if(file_exists(MODULE_PATH.$sModuleName."/".$sModuleName.".php")){
			require_once(MODULE_PATH.$sModuleName."/".$sModuleName.".php");
			
		}else{
			$this->language = $this->sLangDefault;
			$this->module = $this->sModuleDefault;
			$this->loadModule($this->module);
		}
		
	}
}
?>